import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:free_music_player/providers/music_provider.dart';
import 'package:free_music_player/screens/home_screen.dart';
import 'package:free_music_player/services/music_service.dart';
import 'package:free_music_player/widgets/player_bottom_bar.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider<MusicService>(
          create: (_) => MusicService(),
          dispose: (_, service) => service.dispose(),
        ),
        ChangeNotifierProxyProvider<MusicService, MusicProvider>(
          create: (context) => MusicProvider(
            Provider.of<MusicService>(context, listen: false),
          ),
          update: (context, service, previous) => MusicProvider(service),
        ),
      ],
      child: MaterialApp(
        title: 'Free Music Player',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
          useMaterial3: true,
        ),
        home: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    // 初始化音乐服务
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Provider.of<MusicProvider>(context, listen: false).init();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: const HomeScreen(),
      bottomNavigationBar: PlayerBottomBar(),
    );
  }
}
